import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from datetime import datetime,timedelta


class Testalert:

    @pytest.mark.parametrize('alertinfo', YamlUtil().read_testcase_yaml('v24.06/alertmanager/1_get_alert_list.yaml'))
    def test_get_alert_list(self, alertinfo, get_env_info):
        # 获取alert列表接口
        baseUrl = get_env_info + "/clusters/host"
        url = baseUrl + alertinfo['request']['url']
        method = alertinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == alertinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('alertinfo', YamlUtil().read_testcase_yaml('v24.09/alertmanager/2_create_silence_rule.yaml'))
    def test_create_silence_rule(self, alertinfo, get_env_info):
        # 创建静默规则
        baseUrl = get_env_info + "/clusters/host"
        url = baseUrl + alertinfo['request']['url']
        method = alertinfo['request']['method']
        starttime = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
        endtime =(datetime.utcnow() + timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ')
        if alertinfo['request']['data']['startsAt'] != "{starttime}1":
            alertinfo['request']['data']['startsAt']=starttime
        else:
            alertinfo['request']['data']['startsAt'] = starttime + "1"
        if alertinfo['request']['data']['endsAt'] != "{endtime}1":
            alertinfo['request']['data']['endsAt']=endtime
        else:
            alertinfo['request']['data']['startsAt'] = endtime + "1"
        data = alertinfo['request']['data']
        req = RequestUtil().send_request(method, url,json=data)
        if '-' not in alertinfo['name']:
            globals()['silenceId'] = req.json()['silenceID']
        assert req.status_code == alertinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('alertinfo', YamlUtil().read_testcase_yaml('v24.06/alertmanager/3_get_silence_list.yaml'))
    def test_get_silence_rule(self, alertinfo, get_env_info):
        # 获取silence列表接口
        baseUrl = get_env_info + "/clusters/host"
        if '{silenceId}' in alertinfo['request']['url']:
            url = baseUrl + alertinfo['request']['url'].replace('{silenceId}',globals()['silenceId'])
        else:
            url = baseUrl + alertinfo['request']['url']
        method = alertinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == alertinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('alertinfo', YamlUtil().read_testcase_yaml('v24.06/alertmanager/4_delete_silence_rule.yaml'))
    def test_delete_silence_rule(self, alertinfo, get_env_info):
        # 删除静默规则
        baseUrl = get_env_info + "/clusters/host"
        url = baseUrl + alertinfo['request']['url'].replace('{silenceId}',globals()['silenceId'])
        method = alertinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == alertinfo['validate'][0]['equals']['status_code']